package xian.woniuxy.web.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import xian.woniuxy.entity.Type;
import xian.woniuxy.service.ITypeService;
import xian.woniuxy.utils.Pageable;
import xian.woniuxy.utils.ResultVO;

import java.util.List;

@RequestMapping("type")
@RestController
public class TypeController {

    @Autowired
    private ITypeService typeService;

    @GetMapping("/{page}/{size}")
    public ResultVO findAll(@PathVariable int page, @PathVariable int size){
        Pageable all = typeService.findAll((page - 1) * size, size);
        ResultVO resultVO = new ResultVO(200, "find ok", all);
        return resultVO;
    }

    @GetMapping("/{type_name}")
    public ResultVO findByTypeName(@PathVariable String type_name) {
        List<Type> typeList = typeService.findByTypeName(type_name);
        ResultVO resultVO = new ResultVO(200, "find ok", typeList);
        return resultVO;
    }

    @PostMapping
    @ResponseBody
    public String save (@RequestBody Type type){
        System.out.println("type = " + type);
        typeService.save(type);
        return "save ok";
    }

    @DeleteMapping("/{id}")
    public xian.woniuxy.util.ResultVO delete(@PathVariable int id) {
        typeService.deleteById(id);
        return new xian.woniuxy.util.ResultVO(200, "delete ok", null);
    }

    @PutMapping
    public xian.woniuxy.util.ResultVO update(@RequestBody Type type) {
        System.out.println(type);
        typeService.upd(type);
        return new xian.woniuxy.util.ResultVO(200, "update ok", null);
    }
}
